package com.baixun.gassystem.service;

import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @program: 上海燃气驾驶舱
 * @description: 无人机登录api鉴权
 * @author: yesong
 **/

// TODO 接口无法使用
@Service
public class DroneLoginService {
    private static final String LOGIN_URL = "https://wrjxjlocal.sgpnc.com/gis_server_sh/login";

    public  String login() {
        RestTemplate restTemplate = new RestTemplate();

        // 创建静态的请求体
        String requestBody = "{ \"username\": \"13500000001\", \"password\": \"Ktd@2023#357\", \"clientId\": 0 }";

        // 设置请求头，Content-Type 告知服务器数据格式是 JSON
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 构建请求
        HttpEntity<String> request = new HttpEntity<>(requestBody, headers);

        // 发送 POST 请求并获取响应
        ResponseEntity<String> response = restTemplate.exchange(
                LOGIN_URL,
                HttpMethod.POST,
                request,
                String.class
        );

        // 检查响应是否成功
        if (response.getStatusCode().is2xxSuccessful()) {
            // 打印 token 信息
            System.out.println("Response: " + response.getBody());
            return response.getBody();
        } else {
            throw new RuntimeException("登录失败: " + response.getStatusCode());
        }
    }

    public static void main(String[] args) {
        String result = new DroneLoginService().login();
    }
}
